import { fileURLToPath } from 'url'
import path from 'path'
import crypto from 'crypto'

// 判断当前环境是 ESM 还是 CJS
export function isESM() {
  try {
    return typeof import.meta.url === 'string'
  } catch {
    return false
  }
}

export const rootPath = (function () {
  if (isESM()) {
    return path.dirname(fileURLToPath(import.meta.url))
  } else {
    return __dirname
  }
})()

export function getHash(name: string, len: number = 8) {
  return crypto.createHash('md5').update(name).digest('hex').slice(0, len) // 取前8位 hash
}
